package com.fractalist.man.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.fractalist.base.core.utils.MyDateUtils;
import com.fractalist.man.base.SqlUtils;
import com.fractalist.man.service.IPresentScoreService;
import com.fractalist.man.service.IScoreLogService;
import com.fractalist.man.service.IScoreService;
import com.fractalist.man.service.IUserFieldService;
import com.fractalist.man.service.IUserService;

@Service
public class ScoreService implements IScoreService {
	@Autowired
	private IScoreLogService scoreLogService;
	@Autowired
	private IUserService userService;
	@Autowired
	private IPresentScoreService presentScoreService;
	@Autowired
	private IUserFieldService userFieldService;

	@Override
	public boolean addScore(Long curUserId, Integer addScore, Long scoreRuleId) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("userId", curUserId);
		Map<String, Object> user = userService.queryOneRow(SqlUtils
				.genderQuerySql("man_base_user",
						SqlUtils.genderConditionsByType(paramMap, "and")));
		if (user != null && user.size() > 0) {
			try {
				String now = MyDateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss");
				userService.update("update man_base_user set update_time = '"
						+ now + "' where userId = " + curUserId);
				userFieldService
						.update("update man_base_user_field set score = score + "
								+ addScore + " where userId = " + curUserId);
				paramMap = new HashMap<String, Object>();
				paramMap.put("score", addScore);
				paramMap.put("userId", curUserId);
				paramMap.put("scoreRuleId", scoreRuleId);
				paramMap.put("createDate", now);
				scoreLogService.add(SqlUtils.genderInsertSql(
						"man_base_score_log", paramMap));
			} catch (Exception e) {
				TransactionAspectSupport.currentTransactionStatus()
						.setRollbackOnly();
				e.printStackTrace();
				return false;
			}
		}
		return true;
	}

	@Override
	public boolean batchPresentScore(Long userId, Long girlId, Integer score) {
		// 可以将try，catch模块去掉，
		try {
			Map<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("userId", userId);
			paramMap.put("girlId", girlId);
			paramMap.put("createDate",
					MyDateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss"));
			paramMap.put("score", score);
			userFieldService.update("update " + userFieldService.getTableName()
					+ " set score = score + " + score
					+ ",reciveScore = reciveScore + " + score
					+ " where userId = " + girlId);
			userFieldService.update("update " + userFieldService.getTableName()
					+ " set score = score - " + score + " where userId = "
					+ userId);
			presentScoreService.add(SqlUtils.genderInsertSql(
					"man_base_present_score", paramMap));

		} catch (Exception e) {
			TransactionAspectSupport.currentTransactionStatus()
					.setRollbackOnly();
			e.printStackTrace();
			return false;
		}
		return true;
	}
}
